<?php

abstract class AbstractController extends Zend_Controller_Action {
	
	protected $_auth;
	protected $_flashMessenger;
	protected $_title;
	protected $_message;
	protected $_breadcrumb;
	
	public function init() {
		$this->_title = array('Acessos');
		$this->_flashMessenger = $this->getHelper('FlashMessenger');
		$this->_breadcrumb = array('Home' => $this->getRequest()->getBaseUrl());
		$this->_auth = Zend_Auth::getInstance();
		
		$request = $this->getRequest();
		$controller = $request->getControllerName();
		
		if (!$this->_auth->getIdentity() && $controller != 'login')
			$this->_redirect('/login');
		
		if ($this->getRequest()->isXmlHttpRequest() || $this->getRequest()->getParam('ajax', false)) {
			Zend_Layout::getMvcInstance()->disableLayout();
			$this->view->ajax = 1;
		} else
			$this->view->ajax = 0;
	}
	
	public function postDispatch() {
		$this->view->flash_message = $this->getFlashMessages();	
		$this->view->title = $this->_title;
		$this->view->breadcrumb = $this->_breadcrumb;
		$this->view->auth = $this->_auth->getIdentity();
	}
	
	public function addTitle($title) {
		$this->_title[] = $title;
	}
	
	public function addCrumb($crumb, $link = null) {
		if (is_array($crumb))
			foreach ($crumb as $id => $item) {
				$this->_breadcrumb[stripslashes($id)] = $item;
			}
		else
			$this->_breadcrumb[stripslashes($crumb)] = $link;
	}

	public function addMessage($message, $type = 'alert') {
		$this->_flashMessenger->addMessage(array('type' => $type, 'message' =>  $message));
	}
	
	public function addMessageArray($array, $type = 'alert') {
		foreach ($array as $message) {
			$this->addMessage($message, $type);
		}
	}
	
	public function addMessageItemNotFound() {
		$this->addMessage('Item não foi encontrado ou não existe!', 'error');
	}
	
	public function addMessageRequired() {
		$this->addMessage('Campos obrigatórios devem ser preenchidos!', 'error');
	}
	
	public function getFlashMessages() {
		$flash_message = array_merge(
		    $this->_flashMessenger->getMessages(),
		    $this->_flashMessenger->getCurrentMessages()
		);
		$this->_flashMessenger->clearCurrentMessages();
		
		$array = array();
		foreach ($flash_message as $message) {
			$array[] = 'Array("' . $message['type'] . '","' . $message['message'] . '")';
		}
		
		if (count($flash_message))
			return 'Array(' . implode(',', $array) . ')';
		else
			return false;
	}
	
	public function getReferer() {
		$referer = false;
		
		if (isset($_SERVER['HTTP_REFERER']))
			$referer = $_SERVER['HTTP_REFERER'];
		
		return $referer;
	}
	
	public function getBaseUrl() {
		return $this->getRequest()->getBaseUrl();
	}
	
	public function getBasePath() {
		return BASE_PATH;
	}

	public function paginator($data, $count_per_page = 5, $param = 'pagina') {
		Zend_View_Helper_PaginationControl::setDefaultViewPartial('includes/paginacao.phtml');
		
		$paginacao = Zend_Paginator::factory($data);
		$paginacao->setCurrentPageNumber($this->getRequest()->getParam($param));
		$paginacao->setItemCountPerPage($count_per_page);
		
		return $paginacao;
	}

	public function sendMail($mensagem, $assunto, $para = 'adriano-souza@outlook.com', $responder = false, $anexos = false) {
		
		$config = array(
			'auth'		=> 'login',
			'username'	=> 'adcriati',
			'password'	=> 'HftEaIR(US5s'
		);
		
		$transport = new Zend_Mail_Transport_Smtp('mail.adcriativa.com.br', $config);
		$mail = new Zend_Mail();
		$mail->setBodyHtml(utf8_decode($mensagem));
		$mail->setFrom('adriano-souza@outlook.com', 'Adriano Souza - Acessos');
		$mail->setSubject(utf8_decode($assunto));

		if (is_array($para))
			foreach ($para as $email) {
				$mail->addTo($email);
			}
		else
			$mail->addTo($para);

		if ($responder)
			$mail->setReplyTo($responder);

		if ($anexos) {
			if (is_array($anexos)) {
				for ($i = 0 ; $i < sizeof($anexos); $i++) {
					$fileContents = file_get_contents($anexos[$i]); 
					$attachment = $mail->createAttachment($fileContents);
					$attachment->filename = basename($anexos[$i]);
				}
			} else {
				$fileContents = file_get_contents($anexos); 
				$attachment = $mail->createAttachment($fileContents);
				$attachment->filename = basename($anexos);
			}
		}
		
		return $mail->send($transport);
	}
}